package com.falser.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.falser.user.dao.SysUserRoleDao;
import com.falser.user.entity.SysRole;
import com.falser.user.entity.SysUserRole;
import com.falser.user.service.SysRoleService;
import com.falser.user.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户和角色关联表(SysUserRole)表服务实现类
 *
 * @author makejava
 * @since 2021-09-16 20:49:57
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRole> implements SysUserRoleService {

    @Resource
    SysRoleService sysRoleService;

    @Override
    public List<SysRole> getRoleListByUserId(Long userId) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, userId);

        List<SysUserRole> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();
        List<Long> roleIds = list.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        return Optional.of(sysRoleService.listByIds(roleIds)).orElse(new ArrayList<>());
    }
}

